package com.heiye.comment.biz.rpc;

import cn.hutool.core.collection.CollUtil;
import com.heiye.count.api.api.CountFeignApi;
import com.heiye.count.api.dto.*;
import com.heiye.framework.common.response.Response;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author: heiye
 * @date: 2025/07/30 下午6:29
 * @version: v1.0.0
 * @description: 计数服务
 */
@Component
public class CountRpcService {

    @Resource
    private CountFeignApi countFeignApi;

    /**
     * 查询问题计数数据
     *
     * @param quizId
     * @return
     */
    public FindQuizCountsByIdRspDTO findQuizCountsById(Long quizId) {
        FindQuizCountsByIdReqDTO findQuizCountsByIdReqDTO = FindQuizCountsByIdReqDTO.builder()
                .quizId(quizId)
                .build();

        Response<FindQuizCountsByIdRspDTO> response = countFeignApi.findQuizCountData(findQuizCountsByIdReqDTO);

        if (!response.isSuccess() || Objects.isNull(response.getData())) {
            return null;
        }

        return response.getData();
    }

    /**
     * 修改问题评论计数数据
     *
     * @param quizId
     * @param count
     * @return
     */
    public boolean updateQuizCommentCount(Long quizId, int count) {
        UpdateQuizCommentCountReqDTO updateQuizCommentCountReqDTO = UpdateQuizCommentCountReqDTO.builder()
                .quizId(quizId)
                .count(count)
                .build();

        Response<?> response = countFeignApi.updateQuizCommentCountData(updateQuizCommentCountReqDTO);

        if (!response.isSuccess()) {
            throw new RuntimeException("修改问题评论计数失败");
        }

        return true;
    }

    /**
     * 批量查询评论计数数据
     *
     * @param commentIds
     * @return
     */
    public List<FindCommentCountByIdRspDTO> findCommentsCountData(List<Long> commentIds) {

        FindCommentCountByIdsReqDTO findCommentCountByIdsReqDTO = FindCommentCountByIdsReqDTO.builder()
                .commentIds(commentIds)
                .build();

        Response<List<FindCommentCountByIdRspDTO>> response = countFeignApi.findCommentsCountData(findCommentCountByIdsReqDTO);

        if (!response.isSuccess() || Objects.isNull(response.getData()) || CollUtil.isEmpty(response.getData())) {
            return null;
        }

        return response.getData();
    }
}
